package com.gs.m_change_password

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.ImageView
import androidx.core.widget.addTextChangedListener
import com.gs.common.BaseCompatActivity
import com.gs.common.constant.ActionConst
import com.gs.common.global.GlobalVariables
import com.gs.common.manager.ActivityManager
import com.gs.common.util.ActionBarUtil
import com.gs.common.util.DialogUtil
import com.gs.common.util.ToastUtil
import com.gs.m_change_password.databinding.ChangePasswordLayoutBinding

class ChangePasswordActivity : BaseCompatActivity(),
    ChangePasswordPersistent.OnChangePasswordStateCallback {

    private lateinit var binding: ChangePasswordLayoutBinding
    private val changePasswordPersistent by lazy { ChangePasswordPersistent.getInstance(this) }
    private val handler = Handler(Looper.getMainLooper())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ChangePasswordLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 自定义action bar
        ActionBarUtil.setCustomActionBar(this, R.layout.change_password_actionbar_layout)

        // 返回键
        val backBtn = findViewById<ImageView>(R.id.back_btn)
        backBtn.setOnClickListener { finish() }

        binding.curPasswordEdt.addTextChangedListener {
            if (it != null) {
                if (it.toString() == binding.changePasswordEdt.text.toString()) {
                    binding.changePasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.red))
                } else {
                    binding.changePasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.black))
                }
            }
        }

        binding.changePasswordEdt.addTextChangedListener {
            if (it != null) {
                if ((it.toString() == binding.curPasswordEdt.text.toString()) || it.length < 8) {
                    binding.changePasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.red))
                } else {
                    binding.changePasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.black))
                }
            }
        }

        binding.reChangePasswordEdt.addTextChangedListener {
            if (it != null) {
                if (it.toString() != binding.changePasswordEdt.text.toString()) {
                    binding.reChangePasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.red))
                } else {
                    binding.reChangePasswordEdt.setTextColor(resources.getColor(com.gs.common.R.color.black))
                }
            }
        }

        binding.changePasswordBtn.setOnClickListener {
            val loginUser = GlobalVariables.loginUser
            if (loginUser == null) {
                ToastUtil.showShortToast(this, "修改密码失败")
                return@setOnClickListener
            }
            val curPassword = binding.curPasswordEdt.text.toString()
            val changePassword = binding.changePasswordEdt.text.toString()
            val reChangePassword = binding.reChangePasswordEdt.text.toString()

            if (curPassword.isEmpty() || changePassword.isEmpty() || reChangePassword.isEmpty()) {
                ToastUtil.showShortToast(this, "请填写完整信息")
                return@setOnClickListener
            }

            if (curPassword == changePassword) {
                ToastUtil.showShortToast(this, "修改密码不能与当前密码一致")
                return@setOnClickListener
            }
            if (changePassword != reChangePassword) {
                ToastUtil.showShortToast(this, "再次输入的密码需要与修改密码一致")
                return@setOnClickListener
            }

            val alertDialog = DialogUtil.createCommonAlertDialog(this, "修改", "原密码") {
                if (it == DialogUtil.CONFIRM_TO_DO) {
                    changePasswordPersistent.changePassword(
                        loginUser.userName,
                        curPassword,
                        changePassword,
                        this
                    )
                }
            }
            alertDialog.show()
        }
    }

    override fun onChangePasswordSuccess() {
        ToastUtil.showShortToast(this, "修改密码成功，请重新登录")
        ActivityManager.finishAllActivity()
        val intent = Intent().apply {
            action = ActionConst.TO_INDEX_ACTIVITY_ACTION
        }
        startActivity(intent)
    }

    override fun onChangePasswordFailed() {
        ToastUtil.showShortToast(this, "当前密码错误")
    }
}